package com.feisuanyz.service.impl;

import com.feisuanyz.dto.EventManagementDTO;
import com.feisuanyz.entity.EventManagementDO;
import com.feisuanyz.query.EventManagementQuery;
import com.feisuanyz.repository.EventManagementRepository;
import com.feisuanyz.service.EventManagementService;
import com.feisuanyz.util.RestResult;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 事件管理服务实现类
 * @author 23265
 */
@Slf4j
@Service
public class EventManagementServiceImpl implements EventManagementService {

    @Autowired
    private EventManagementRepository eventManagementRepository;

    @Override
    @Transactional
    public RestResult createEvent(EventManagementDTO eventManagementDTO) {
        try {
            EventManagementDO eventManagementDO = new EventManagementDO();
            BeanUtils.copyProperties(eventManagementDTO, eventManagementDO);
            eventManagementDO.setCreateTime(new Date());
            eventManagementRepository.save(eventManagementDO);
            return RestResult.success("调用成功", eventManagementDTO);
        } catch (Exception e) {
            log.error("创建事件失败: {}", e.getMessage());
            return RestResult.failure("调用失败");
        }
    }

    @Override
    @Transactional
    public RestResult updateEvent(EventManagementDTO eventManagementDTO) {
        try {
            Optional<EventManagementDO> optionalEventManagementDO = eventManagementRepository.findById(eventManagementDTO.getEventId());
            if (!optionalEventManagementDO.isPresent()) {
                return RestResult.failure("事件不存在");
            }
            EventManagementDO eventManagementDO = optionalEventManagementDO.get();
            BeanUtils.copyProperties(eventManagementDTO, eventManagementDO, "eventId", "createTime");
            eventManagementDO.setUpdateTime(new Date());
            eventManagementRepository.save(eventManagementDO);
            return RestResult.success("调用成功", eventManagementDTO);
        } catch (Exception e) {
            log.error("编辑事件失败: {}", e.getMessage());
            return RestResult.failure("调用失败");
        }
    }

    @Override
    @Transactional
    public RestResult deleteEvent(Integer eventId, Integer updateBy) {
        try {
            Optional<EventManagementDO> optionalEventManagementDO = eventManagementRepository.findById(eventId);
            if (!optionalEventManagementDO.isPresent()) {
                return RestResult.failure("事件不存在");
            }
            EventManagementDO eventManagementDO = optionalEventManagementDO.get();
            eventManagementRepository.deleteById(eventId);
            return RestResult.success("调用成功");
        } catch (Exception e) {
            log.error("删除事件失败: {}", e.getMessage());
            return RestResult.failure("调用失败");
        }
    }

    @Override
    public RestResult findEvents(EventManagementQuery query) {
        try {
            List<EventManagementDO> eventManagementDOList = eventManagementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                criteriaQuery.distinct(true);
                var predicates = new ArrayList<>();

                if (query.getTitle() != null) {
                    predicates.add(criteriaBuilder.like(root.get("title"), "%" + query.getTitle() + "%"));
                }

                if (query.getDescription() != null) {
                    predicates.add(criteriaBuilder.like(root.get("description"), "%" + query.getDescription() + "%"));
                }

                if (query.getLocation() != null) {
                    predicates.add(criteriaBuilder.like(root.get("location"), "%" + query.getLocation() + "%"));
                }

                if (query.getVenue() != null) {
                    predicates.add(criteriaBuilder.like(root.get("venue"), "%" + query.getVenue() + "%"));
                }

                if (query.getEventDate() != null) {
                    predicates.add(criteriaBuilder.equal(root.get("eventDate"), query.getEventDate()));
                }

                if (query.getEventTime() != null) {
                    predicates.add(criteriaBuilder.equal(root.get("eventTime"), query.getEventTime()));
                }

                return criteriaBuilder.and(predicates.toArray(new javax.persistence.criteria.Predicate[0]));
            });

            List<EventManagementDTO> eventManagementDTOList = new ArrayList<>();
            eventManagementDOList.forEach(eventManagementDO -> {
                EventManagementDTO eventManagementDTO = new EventManagementDTO();
                BeanUtils.copyProperties(eventManagementDO, eventManagementDTO);
                eventManagementDTOList.add(eventManagementDTO);
            });

            return RestResult.success("调用成功", eventManagementDTOList);
        } catch (Exception e) {
            log.error("查询事件失败: {}", e.getMessage());
            return RestResult.failure("调用失败");
        }
    }
}